<?php

namespace app\admin\utils\controller;

use app\admin\utils\model\UtilLangDetail;
use support\Request;
use support\Response;
use app\admin\utils\model\UtilLang;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use app\common\services\DragdoneUpService;

/**
 * 语言管理 
 */
class UtilLangController extends Crud
{
    
    /**
     * @var UtilLang
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new UtilLang;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        // 将url参数传递给前端
        // \support\View::assign(['params'=>$request->get()]);
        $list = UtilLang::whereRaw('1')->pluck('code')->toArray();
//        var_dump($list);
        return view('utils/util-lang/index',['data'=>"'".join("','",$list)."'"]);
    }

    public function afterQuery($items)
    {
        $max = UtilLangDetail::where(['lang_id'=>0])->count();
        foreach ($items as $k=>$v){
            $clent = UtilLangDetail::where(['lang_id'=>$v->id])->count();
            $items[$k]['number'] = $clent.'/'.$max;
        }
        return $items;
    }

    // 创建语言包
    public function generate(){
        UtilLang::generate();
        return json(['code'=>0,'msg'=>'更新成功']);
    }



    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('utils/util-lang/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
                    // 拖拽排序更新
            if($request->post('dragDone')==1 && DragdoneUpService::dragDoneUpData($request,$this->model)){
                return $this->json(0,'ok');
            }
            return parent::update($request);
        }
        return view('utils/util-lang/update');
    }

}
